
Constructor overloading vary much similar to method over loading but it uses Constructor. Based on parameter constructor behaver are change then it called constructor overloading. Constructor look like method but it doesn’t have return type and it similar to class name. Now Given bellow the Constructor overloading code and explain the code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using testForClass1;
namespace testFor
{
public class Program
{
static void Main(string[] args)
{
Student std = new Student();
Student std1 = new Student("Farhan");
Student std2 = new Student("Farhan", "Sakib");
Console.Read();
}
}
public class Student
{
public Student()
{
Console.WriteLine("No argument is pass");
}
public Student(string firstName)
{
Console.WriteLine(firstName);
}
public Student(string firstName, string lastName)
{
Console.WriteLine(firstName + " " + lastName);
}
}
}
We can see the student class contain the three-constructor same name as class. It looks like method but no return type. This constructor’s parameter is deferent then under main method we call the contracture by the parameter changing. Parameter change then contracture behaver change we can see the output.